<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-4-8
 * Time: 上午9:28
 */

namespace backend\modules\android_app_versions\controllers;


use backend\modules\android_app_versions\models\AndroidAppUploadForm;
use yii\web\Controller;
use yii\web\UploadedFile;

class DefaultController extends Controller
{
    public function actionIndex()
    {
        $model = new AndroidAppUploadForm();
        if (\Yii::$app->request->isPost) {
            $apk = UploadedFile::getInstance($model, 'apk');
            if (file_exists($apk)){
                unlink($apk);
            }
            $file = \Yii::getAlias('@data/attsort/app-carloan-release.apk');
            if ($apk->saveAs($file)) {
                // 文件上传成功
                \Yii::$app->session->addFlash('success', "上传成功!");
            }else{
                \Yii::$app->session->addFlash('error', "上传失败!");
            }
        }
        return $this->render('index', [
            'model' => $model,
        ]);
    }

    public function actionUploadOtherVersion()
    {
        $model = new AndroidAppUploadForm();
        if (\Yii::$app->request->isPost) {
            $apk = UploadedFile::getInstance($model, 'apk');
            if (file_exists($apk)){
                unlink($apk);
            }
            $file = \Yii::getAlias('@data/attsort/app-carloan-other.apk');
            if ($apk->saveAs($file)) {
                // 文件上传成功
                \Yii::$app->session->addFlash('success', "上传成功!");
            }else{
                \Yii::$app->session->addFlash('error', "上传失败!");
            }
        }
        return $this->render('upload-other-version', [
            'model' => $model,
        ]);
    }
}